﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Input;
using MyOS.Models;
using MyOS.Utils;
using MyOS.ViewModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;

namespace MyOS.Views.ChildView
{
    /// <summary>
    ///     MoveFileVIew.xaml 的交互逻辑
    /// </summary>
    public partial class MoveFileVIew : WindowX
    {
        private readonly FileStream readFileStream;
        private readonly FileViewModel fileViewModel;
        private readonly char originDiskName;
        private readonly FileTreeViewItemModel originFile;
        private char targetDiskName;


        public MoveFileVIew(FileTreeViewItemModel originFile, char originDiskName, FileViewModel ViewModel)
        {
            this.originFile = originFile;
            this.originDiskName = originDiskName;
            InitializeComponent();
            Block.Text = "将 " + originFile.FileName + " 移动至:";
            DataContext = ViewModel;
            fileViewModel = ViewModel;
        }

        private void TvMenu_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            var item = (FileTreeViewItemModel) TvMenu.SelectedItem;
            try
            {
                if (item.FileName.Equals("本地磁盘C"))
                    targetDiskName = 'C';
                else if (item.FileName.Equals("本地磁盘D")) targetDiskName = 'D';
            }
            catch (Exception exception)
            {
                e.Handled = false;
            }
        }


        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Notice.Show("未改动！", "Info", 3, MessageBoxIcon.Info);
            Close();
        }

        private void ButtonOk_Click(object sender, RoutedEventArgs e)
        {
            var targetFile = (FileTreeViewItemModel) TvMenu.SelectedItem;
            if (targetFile.TypeName.Equals('e')) //step1:先判断是不是在目录中移动
            {
                MessageBoxX.Show("只能在目录中移动!", "Error", Application.Current.MainWindow,
                    MessageBoxButton.OK, new MessageBoxXConfigurations
                    {
                        OKButton = "好的",
                        MinHeight = 180,
                        MinWidth = 300,
                        MessageBoxIcon = MessageBoxIcon.Error,
                        ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                    });
            }
            else if (targetFile.FileName.Equals("此电脑")) //不能在祖先目录移动
            {
                MessageBoxX.Show("请选择磁盘!", "Error", Application.Current.MainWindow,
                    MessageBoxButton.OK, new MessageBoxXConfigurations
                    {
                        OKButton = "好的",
                        MinHeight = 180,
                        MinWidth = 300,
                        MessageBoxIcon = MessageBoxIcon.Error,
                        ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                    });
            }
            else //移动目标为目录
            {
                if (targetDiskName.Equals(originDiskName)) //如果文件原位置与目标位置的磁盘一样
                {
                    deleteFromModel(originFile.FileName, originFile.TypeName,
                        originDiskName == 'C' ? fileViewModel.DiskCModel : fileViewModel.DiskDModel);
                    targetFile.MenuItems.Add(new FileTreeViewItemModel(originFile.FileName, originFile.TypeName,
                        originFile.Icon));
                    Notice.Show("移动成功！", "Success", 3, MessageBoxIcon.Success);
                    Close();
                }
                else
                {
                    if (DiskUtils.searchIsExists(targetDiskName, originFile.FileName, originFile.TypeName) ==
                        -1) //等于-1 不存在
                    {
                        DiskUtils.readFile(originDiskName, originFile.FileName); //从原磁盘读文件
                        var fileBytes = DiskUtils.FileBytes;
                        var startBlock = DiskUtils.findEmptyBlock(targetDiskName, fileBytes.Length);
                        if (startBlock == -1) //判断目标磁盘空间剩余 -1说明空间不足
                        {
                            MessageBoxX.Show("目标磁盘空间不足，移动失败!", "Error", Application.Current.MainWindow,
                                MessageBoxButton.OK, new MessageBoxXConfigurations
                                {
                                    OKButton = "好的",
                                    MinHeight = 180,
                                    MinWidth = 300,
                                    MessageBoxIcon = MessageBoxIcon.Error,
                                    ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                                });
                        }
                        else
                        {
                            deleteFromModel(originFile.FileName, originFile.TypeName,
                                originDiskName == 'C' ? fileViewModel.DiskCModel : fileViewModel.DiskDModel);
                            var attrBytes = DiskUtils.readFileAttribute(originDiskName,
                                DiskUtils.searchIsExists(originDiskName, originFile.FileName, originFile.TypeName));
                            // 在原文件的目录中查找文件属性
                            DiskUtils.delete(originDiskName, originFile.FileName, originFile.TypeName); //先把文件从原磁盘删除
                            attrBytes[5] = Convert.ToByte(startBlock); //将起始盘块号更改
                            DiskUtils.writeToRoot(targetDiskName, attrBytes);
                            DiskUtils.writeToFatAndDisk(targetDiskName, fileBytes, startBlock);
                            targetFile.MenuItems.Add(new FileTreeViewItemModel(originFile.FileName, originFile.TypeName,
                                originFile.Icon));
                            Notice.Show("移动成功！", "Success", 3, MessageBoxIcon.Success);
                            Close();
                        }
                    }
                    else
                    {
                        MessageBoxX.Show("目标磁盘存在同名文件!", "Error", Application.Current.MainWindow,
                            MessageBoxButton.OK, new MessageBoxXConfigurations
                            {
                                OKButton = "好的",
                                MinHeight = 180,
                                MinWidth = 300,
                                MessageBoxIcon = MessageBoxIcon.Error,
                                ButtonBrush = "#FF4C4C".ToColor().ToBrush()
                            });
                    }
                }
            }
        }

        private void deleteFromModel(string fileName, char fileType, FileTreeViewItemModel item)
        {
            for (var i = 0; i < item.MenuItems.Count; i++)
                if (item.MenuItems[i].FileName == fileName && item.MenuItems[i].TypeName.Equals(fileType)) //找到了文件/文件夹
                {
                    item.MenuItems.RemoveAt(i);
                    return;
                }
                else
                {
                    deleteFromModel(fileName, fileType, item.MenuItems[i]);
                }
        }
    }
}